package cn.kailangtu.commontools;

import android.util.Log;

import java.math.BigInteger;

/**
 * Description: 工具类
 */

public class CommonUtils {

    static String TAG = "CommonUtils==>";
    public static String formatHexString(String hexString) {
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }

        if (hexString.length() < 4) {
            hexString = "00" + hexString;
        }
        return hexString;
    }

    /**
     * 16进制字符串转2进制
     *
     * @param s
     * @return
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    /**
     * 二进制转16进制字符串
     *
     * @param b
     * @return
     */
    public static String ByteArrayToHexString(byte[] b) {
        String a = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            a = a + hex;
        }

        return a.toUpperCase();
    }

    public static byte[] ArrayAppend(byte[] paramArrayOfByte1,
                                     byte[] paramArrayOfByte2, int paramInt) {
        byte[] arrayOfByte;
        if ((paramArrayOfByte1 == null) && (paramArrayOfByte2 == null)) {
            arrayOfByte = null;
            return arrayOfByte;
        }
        if (paramArrayOfByte1 == null) {
            arrayOfByte = new byte[paramInt];
            System.arraycopy(paramArrayOfByte2, 0, arrayOfByte, 0, paramInt);
            return arrayOfByte;
        }
        if (paramArrayOfByte2 == null) {
            arrayOfByte = new byte[paramArrayOfByte1.length];
            System.arraycopy(paramArrayOfByte1, 0, arrayOfByte, 0,
                    paramArrayOfByte1.length);
            return arrayOfByte;
        }
        arrayOfByte = new byte[paramInt + paramArrayOfByte1.length];
        System.arraycopy(paramArrayOfByte1, 0, arrayOfByte, 0,
                paramArrayOfByte1.length);
        System.arraycopy(paramArrayOfByte2, 0, arrayOfByte,
                paramArrayOfByte1.length, paramInt);
        return arrayOfByte;
    }

    /**
     * 把指令格式的ascii码解析成字符
     *
     * @param string
     * @return
     */
    public static String decodeForAscii(String string) {
        StringBuffer sb = new StringBuffer();
        byte[] array = CommonUtils.hexStringToByteArray(string);
        for (int i = 0; i < array.length; i++) {
            char c = (char) array[i];
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 将字节数组转换为整型~
     *
     * @return
     */
    //byte 数组与 int 的相互转换
    public static int byteArrayToInt(byte[] b) {
        return b[3] & 0xFF |
                (b[2] & 0xFF) << 8 |
                (b[1] & 0xFF) << 16 |
                (b[0] & 0xFF) << 24;
    }

    /**
     * 将字节数组转换为整型~
     *
     * @return
     */
    //byte 数组与 int 的相互转换
    public static int byteArrayToInt16(byte[] b) {
        return b[1] & 0xFF |
                (b[0] & 0xFF) << 8;

    }

    public static int byteArrayToInt16Temp(byte[] b) {
        return ( b[0] << 8) + (b[1]& 0xff);

    }

    public static byte[] int2ByteArray(int a) {
        return new byte[]{
                (byte) ((a >> 8) & 0xFF),
                (byte) (a & 0xFF)
        };
    }


    public static byte[] int1ByteArray(int a) {
        return new byte[]{
                (byte) (a & 0xFF)
        };
    }


    public static int hexToInt(String hex) {
        BigInteger bi = new BigInteger(hex, 16);
        int no = bi.intValue();
        return no;
    }

    public static String getCheckValue(String cmdSource){
        String cmd = cmdSource.substring(0,cmdSource.length());
        String checkString = "";
        if (cmd.length() % 2 == 0) {
//            AppStaticMethods.kLog(TAG + "计算校验的字符串合法：" + cmd);
            int noCount = 0;
            for (int i = 0; i < cmd.length() / 2; i++) {
                String str = cmd.substring(i * 2, (i + 1) * 2);
                int noValue = CommonUtils.hexToInt(str);
                noCount += noValue;
            }
            checkString =  CommonUtils.ByteArrayToHexString(int1ByteArray(noCount));
//            AppStaticMethods.kLog(TAG + "校验：" + str校验);

            if(checkString.length() > 2) {
                checkString = checkString.substring(checkString.length() - 2, checkString.length());
                checkString = checkString.toUpperCase();
            }
            else
            {
//                Log.e(TAG,"cmdSource:"+cmdSource);
//                Log.e(TAG,"checkString:"+checkString);
//                Log.e(TAG,"noCount:"+noCount);
            }
        }
        return checkString.toUpperCase();
    }

    public static boolean check(String cmdSource){
        if(cmdSource != null && cmdSource.length() >= 16) {
            String cmd = cmdSource.substring(4, cmdSource.length() - 6);
            String checkSource = cmdSource.substring(cmdSource.length() - 6, cmdSource.length() - 4).toUpperCase();
            String computeCheck = getCheckValue(cmd);
            return checkSource.equals(computeCheck);
        }
        return false;
    }

    //合并成int
    public static int combineBytesToInt(byte byte1, byte byte2) {
        // 将byte1左移8位，空出的低位由byte2填充
        return (byte1 << 8) | (byte2 & 0xFF);
    }
    //合并成int 无符号数
    public static int bytesToUnsignedInt(byte high, byte low) {
        return ((high & 0xFF) << 8) | (low & 0xFF);
    }

}
